﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Ymatou.User.Dto;
using YmtSystem.CrossCutting;

namespace YmtAuth.Domain.Model.LoginAuth
{
    /// <summary>
    /// 用户凭据验证规则
    /// </summary>
    public class BillVerifySpecifications
    {
        /// <summary>
        ///  验证登录名（昵称）
        /// </summary>    
        public static UserLoginBillsVerifyCode VerifyLoginId(string loginId)
        {
            //验证是否空
            if (loginId.IsEmpty()) return UserLoginBillsVerifyCode.LoginNameEmpty;
            //验证长度
            if (VerifyLoginIdLength(loginId) != UserLoginBillsVerifyCode.Success) return UserLoginBillsVerifyCode.LoginNameLengthError;
            //验证内容
            var re = new Regex(@"^[\u4e00-\u9fa5\w]+$");
            if (!loginId.EndsWith(".weixin") && !loginId.EndsWith(".alipay") && !loginId.EndsWith(".weibo") && !re.IsMatch(loginId)) return UserLoginBillsVerifyCode.LoginNameContextError;
            //验证首末位字符
            if (loginId.StartsWith("_") || loginId.EndsWith("_")) return UserLoginBillsVerifyCode.LoginNameFistLastError;
            //验证是否为纯数字
            var regNums = new Regex(@"^[\d]+$");
            if (regNums.IsMatch(loginId)) return UserLoginBillsVerifyCode.LoginNameFullNumberError;
            //验证是否包含不允许的字符
            if (loginId.Contains("码头")
                || loginId.ToLower().Contains("ymatou")
                || loginId.ToLower().Contains("yangmatou")
                || loginId.ToLower().Contains("ymt")
                ) return UserLoginBillsVerifyCode.LoginNameContainErrorCode;

            return UserLoginBillsVerifyCode.Success;
        }

        /// <summary>
        /// 验证登录mail
        /// </summary>      
        public static UserLoginBillsVerifyCode VerifyLoginMail(string mail)
        {
            if (mail.IsEmpty()) return UserLoginBillsVerifyCode.EmailNullError;
            if (!Regex.IsMatch(mail, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")) return UserLoginBillsVerifyCode.EmailFormartError;

            return UserLoginBillsVerifyCode.Success;
        }

        /// <summary>
        /// 验证登录手机号码
        /// </summary>      
        public static UserLoginBillsVerifyCode VerifyMobile(string mobile)
        {
            if (mobile.IsEmpty()) return UserLoginBillsVerifyCode.MobileFormartError;
            //@"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$"
            if (!Regex.IsMatch(mobile, @"^[1]+[3,5,7,8]+\d{9}")) return UserLoginBillsVerifyCode.MobileFormartError;

            return UserLoginBillsVerifyCode.Success;
        }

        /// <summary>
        /// 验证登录名长度
        /// </summary>      
        public static UserLoginBillsVerifyCode VerifyLoginIdLength(string loginId)
        {
            int loginIdLenth = 0;

            if (!string.IsNullOrEmpty(loginId))
            {
                //验证长度
                loginIdLenth = Encoding.Default.GetBytes(loginId).Length;                
            }

            if (loginIdLenth <= 3 || loginIdLenth > MaxLoginIdLength) return UserLoginBillsVerifyCode.LoginNameLengthError;

            return UserLoginBillsVerifyCode.Success;
        }

        private static int maxLoginIdLength = 0;
        /// <summary>
        /// 登录名的最大允许长度
        /// </summary>
        public static int MaxLoginIdLength
        {

            get
            {
                if (maxLoginIdLength <= 0)
                {
                    var _MaxLoginIdLength = System.Configuration.ConfigurationManager.AppSettings["MaxLoginIdLength"];
                    maxLoginIdLength = _MaxLoginIdLength.ConvertToInt32(40);
                }
                return maxLoginIdLength;
            }
        }
    }
}
